package com.bwie.orders.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String QUEUE_NAME = "order_queue"; // 队列名称
    public static final String EXCHANGE_NAME = "order_exchange"; // 交换机名称
    public static final String ROUTING_KEY = "order_routing_key"; // 路由键

    // 定义队列
    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, false); // 第二个参数表示是否持久化
    }

    // 定义主题交换机
    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    // 绑定队列到交换机上
    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}
